	<template>
		<view>
			<uni-search-bar placeholder="请输入您想要的物品" radius="20" @confirm="search"></uni-search-bar>
			<view style="height: 50rpx;display: flex;">
				<scroll-view scroll-x style="width: 570rpx;white-space: nowrap;">
					<view style="margin-top: 6rpx;">
						<uni-tag circle text="价格从低到高" class="tag" type="primary" inverted @click="typechange(0)"></uni-tag>
						<uni-tag circle text="价格从高到低" class="tag" type="success" inverted @click="typechange(1)"></uni-tag>
						<uni-tag circle text="最新发布" class="tag" type="default" inverted @click="typechange(2)"></uni-tag>
						<uni-tag circle text="综合排序" class="tag" type="warning" inverted @click="typechange(3)"></uni-tag>
					</view>
				</scroll-view>
				<view class="topview">
					<uni-icons type="paperplane" size="16"></uni-icons>
					<text style="margin-left: 10rpx;">{{school}}</text>
				</view>
			</view>
			<view style="margin: 250rpx 0rpx;text-align: center;"  v-if="productList[0]==null">
				<image src="../static/zanwu.png" class="img3"></image>
			</view>
			<view class="cardview" @click="go(i.id,i.userid)" v-for="i in productList">
				<image :src="i.img[0]" class="img1" mode="aspectFill"></image>
				<view class="titleview">{{i.title}}</view>
				<view>
					<text style="font-size: 26rpx;color: red;font-weight: 700;">￥</text>
					<text style="font-size: 40rpx;color: red;font-weight: 700;">{{i.price}}</text>
				</view>
				<view class="view2">
					<image :src="i.user.avatar!=''?baseUrl+i.user.avatar:'../static/yonghu.png'" class="img2"></image>
					<view class="name1">{{i.user.nickName}}</view>
				</view>
				<view class="date">发布日期:{{i.createDate}}</view>
			</view>
			<view style="float: left;height: 50rpx;width: 750rpx;"></view>
			<uni-popup ref="popup">
				<view class="popup">
					<uni-title type="h1" title="请选择您的学校" align="center"></uni-title>
					<uni-combox placeholder="请选择学校" v-model="school" :candidates="schoolList" :border="false"></uni-combox>
					<button class="btn" size="mini" @click="cancel">取消</button>
					<button class="btn" size="mini" type="primary" @click="ensure">确定</button>
				</view>
			</uni-popup>
		</view>
	</template>

	<script>
import { addschoolname, selectproductlist, selectschoollist, selectschoolname } from '../api/myapi'
import config from '@/config'
		export default {
			data() {
				return {
					type:[
						{value:0,text:"价格从低往高"},
						{value:1,text:"价格从高往低"},
						{value:2,text:"最新发布时间"},
						{value:3,text:"综合排序"}
					],
					school:'',
					schoolList:[],
					productList:[],
					baseUrl:config.baseUrl
				}
			},
			methods: {
				async typechange(i){
					if (i==0) {
						this.productList = this.productList.sort((a,b)=>{
							return a.price - b.price
						})
					}else if(i==1){
						this.productList = this.productList.sort((a,b)=>{
							return b.price - a.price
						})
					}else if(i==2){
						this.productList = this.productList.sort((a,b)=>{
							return b.createDate > a.createDate?1:-1
						})
					}else{
						let {data:res1} = await selectproductlist()
						this.productList = res1
					}
				},
				async search(i){
					this.$modal.loading("正在加载，请耐心等待...")
					let {data:res} = await selectproductlist({title:i.value})
					this.productList = res
					this.$modal.closeLoading()
				},
				go(i,userid) {
					console.log(i);
					this.$tab.navigateTo('/pages/xz/detail?id='+i+"&userid="+userid)
				},
				cancel(){
					this.$refs.popup.close()
					this.school = ''
				},
				async ensure(){
					this.$refs.popup.close()
					let r = await addschoolname({userSchool:this.school})
				}
			},
			async onShow() {
				let {data:res1} = await selectproductlist()
				this.productList = res1
				let r = await selectschoolname()
				if (r.data!=null) {
					this.school = r.data.userSchool
				}else{
					let r1 = await selectschoollist()
					this.schoolList = r1.data.map((a)=>{
						return a.schoolname
					})
					this.$refs.popup.open("center")
				}
			}
		}
	</script>

	<style>
		@font-face {
			font-family: 'font1';
			src: url('@/static/font/font1.otf');
		}
		.tag {
			margin-left: 20rpx;
			height: 60rpx;
		}
		.img3 {
			width: 350rpx;
			height: 350rpx;
		}
		.btn {
			width: 40%;
			height: 60rpx;
			margin: 30rpx 0rpx 10rpx 40rpx;
		}
		.popup {
			height: 300rpx;
			width: 600rpx;
			background-color: white;
			border-radius: 20rpx;
			padding: 10rpx;
		}
		
		.date {
			font-size: 22rpx;
			color: #c0c0c0;
		}

		.name1 {
			font-size: 26rpx;
			font-family: '幼圆';
			margin-left: 10rpx;
		}

		.view2 {
			display: flex;
			align-items: center;
		}

		.img2 {
			border-radius: 50%;
			width: 40rpx;
			height: 40rpx;
			margin-top: 10rpx;
			border: 1rpx solid #c0c0c0;
		}

		.titleview {
			font-size: 30rpx;
			font-family: 'font1';
			font-weight: 700;
			height: 94rpx;
			overflow: hidden;
		}

		.cardview {
			width: 47%;
			height: 550rpx;
			box-shadow: 3rpx 3rpx 8rpx rgba(0, 0, 0, .3);
			margin-left: 13rpx;
			margin-top: 10rpx;;
			float: left;
			padding: 10rpx;
			border-radius: 8rpx;
			background-color: white;
		}

		.img1 {
			width: 100%;
			height: 300rpx;
			border-radius: 10rpx;
		}

		.topview {
			font-size: 20rpx;
			margin-left: 10rpx;
		}
	</style>